<?php
/**
 * @title 140. 单词拆分 II - 递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param String $s
     * @param String[] $wordDict
     * @return String[]
     */
    function wordBreak($s, $wordDict) {
        /**
         * @since 2020-11-02 长度
         */
        $lend = count($wordDict);

        /**
         * @since 2020-11-02 哈希字典
         */
        $this->hashs = [];
        $lens = [];
        for($i=0; $i<$lend; $i++){
            $this->hashs[$wordDict[$i]] = 1;
            $lens[strlen($wordDict[$i])] = 1;
        }

        /**
         * @since 2020-11-02 长度
         */
        $this->lens = array_keys($lens);
        sort($this->lens);

        /**
         * @since 2020-11-02 递归
         */
        $result = self::find($s, strlen($s));

        /**
         * @return
         */
        return $result;
    }

    /**
     * @title 返回单词组成语句
     * @author start2004
     * @since 2020-11-02 Created
     *
     * @param string $s
     * @param int $len
     * @param string $wordStr
     * @return array
     */
    public function find($s='', $len=0, $wordStr = ''){
        /**
         * @since 2020-11-02 结果集
         */
        $result = [];

        /**
         * @since 2020-11-02 长度依次匹配
         */
        foreach ($this->lens as $lend){
            if($lend <= $len){
                /**
                 * @since 2020-11-02 lend长度的单词是否存在
                 */
                $word = substr($s, 0, $lend);
                if(isset($this->hashs[$word])){
                    $nextWordStr = $wordStr;
                    $nextWordStr .= " ". $word;

                    /**
                     * @since 2020-11-02 合并结果
                     */
                    if($len == $lend){
                        $result[] = substr($nextWordStr, 1);
                    } else {
                        $result = array_merge($result, self::find(substr($s, $lend), $len-$lend, $nextWordStr));
                    }
                } else {}
            } else {
                break;
            }
        }

        /**
         * @return
         */
        return $result;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202011/2020.11.01.php
 */
$datas = [
    ["catsanddog", ["cat", "cats", "and", "sand", "dog"]],
    // ["pineapplepenapple", ["apple", "pen", "applepen", "pine", "pineapple"],],
    // ["catsandog", ["cats", "dog", "sand", "and", "cat"],],
    // ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->wordBreak(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
